﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace YiSha.Util
{
    /// <summary>
    /// 监控
    /// </summary>
    public class MonitorUtil
    {
        public static MachineMonitorInfo GetMachineMonitorInfo()
        {
            MachineMonitorInfo info = new MachineMonitorInfo();
            ArrayList listThread = new ArrayList();
            // 获取CPU占用比
            Thread thread1 = new Thread(delegate () { GetCpu(ref info); });
            thread1.Start();
            listThread.Add(thread1);
            // 获取CPU核数
            Thread thread2 = new Thread(delegate () { GetCpuCount(ref info); });
            thread2.Start();
            listThread.Add(thread2);
            // 获取可用内存
            Thread thread3 = new Thread(delegate () { GetMenoryAvaliable(ref info); });
            thread3.Start();
            listThread.Add(thread3);
            // 获取总内存
            Thread thread4 = new Thread(delegate () { GetMenoryTotal(ref info); });
            thread4.Start();
            listThread.Add(thread4);
            // 获取C盘可用空间
            Thread thread5 = new Thread(delegate () { GetCAvaliable(ref info); });
            thread5.Start();
            listThread.Add(thread5);
            // 获取C盘总空间
            Thread thread6 = new Thread(delegate () { GetCTotal(ref info); });
            thread6.Start();
            listThread.Add(thread6);
            // 获取D盘可用空间
            Thread thread7 = new Thread(delegate () { GetDAvaliable(ref info); });
            thread7.Start();
            listThread.Add(thread7);
            // 获取D盘总空间
            Thread thread8 = new Thread(delegate () { GetDTotal(ref info); });
            thread8.Start();
            listThread.Add(thread8);
            foreach (Thread thread in listThread)
            {
                thread.Join();
            }
            return info;
        }
        public static void GetCpu(ref MachineMonitorInfo info)
        {
            lock (info)
            {
                info.CpuUsage = CPUMonitor.getValue();
            }
        }
        public static void GetCpuCount(ref MachineMonitorInfo info)
        {
            lock (info)
            {
                info.CoreNumber = ProcessorCountMonitor.getValue();
            }
        }
        public static void GetMenoryAvaliable(ref MachineMonitorInfo info)
        {
            lock (info)
            {
                info.MemoryAvailable = (MemoryMonitor.getMemoryInfo().availPhys / (1024 * 1024));
            }
        }
        public static void GetMenoryTotal(ref MachineMonitorInfo info)
        {
            lock (info)
            {
                info.PhysicalMemory = (MemoryMonitor.getMemoryInfo().totalPhys / (1024 * 1024));
            }
        }
        public static void GetCAvaliable(ref MachineMonitorInfo info)
        {
            lock (info)
            {
                info.CHardDiskFreeSpace = (MemoryMonitor.GetHardDiskFreeSpace("C") / (1024 * 1024));
            }
        }
        public static void GetCTotal(ref MachineMonitorInfo info)
        {
            lock (info)
            {
                info.CHardDiskSpace = (MemoryMonitor.GetHardDiskSpace("C") / (1024 * 1024));
            }
        }
        public static void GetDAvaliable(ref MachineMonitorInfo info)
        {
            lock (info)
            {
                info.DHardDiskFreeSpace = (MemoryMonitor.GetHardDiskFreeSpace("D") / (1024 * 1024));
            }
        }
        public static void GetDTotal(ref MachineMonitorInfo info)
        {
            lock (info)
            {
                info.DHardDiskSpace = (MemoryMonitor.GetHardDiskSpace("D") / (1024 * 1024));
            }
        }
    }
    /// <summary>
    /// 服务器监控信息
    /// </summary>
    [Serializable]
    public class MachineMonitorInfo
    {
        /// <summary>
        /// CPU使用率
        /// </summary>
        public float CpuUsage { get; set; }
        /// <summary>
        /// CPU核心数量
        /// </summary>
        public int CoreNumber { get; set; }
        /// <summary>
        /// 可用内存
        /// </summary>
        public float MemoryAvailable { get; set; }
        /// <summary>
        /// 物理内存
        /// </summary>
        public float PhysicalMemory { get; set; }
        /// <summary>
        /// C盘可用空间
        /// </summary>
        public float CHardDiskFreeSpace { get; set; }
        /// <summary>
        /// D盘可用空间
        /// </summary>
        public float DHardDiskFreeSpace { get; set; }
        /// <summary>
        /// C盘存储空间
        /// </summary>
        public float CHardDiskSpace { get; set; }
        /// <summary>
        /// D盘存储空间
        /// </summary>
        public float DHardDiskSpace { get; set; }
    }
}
